#include <bits/stdc++.h>
using namespace std;
vector <char> a[25];
int n,m,x[5],t,now[505],b[505],q[505],ans[505];
int main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);

    ios::sync_with_stdio(false);
    cin.tie(0);

    cin>>n>>m;
    for (int i=1;i<=n;i++)
    {
        cin>>b[i];
    }
    for (int i=1;i<=m;i++)
    {
        cin>>q[i];
    }
    int ans6=1,num=0;
    while(ans6<n)
    {
        ans6*=2;
        num++;
    }
    //cout<<num<<" "<<"ok"<<endl;
    for (int i=1;i<=num;i++)
    {
        for (int j=1;j<=pow(2,num-i);j++)
        {
            char X;
            cin>>X;
            a[i].push_back(X);
        }
        //cout<<a[i].size()<<" "<<"ok"<<'\n';
    }
    cin>>t;
    queue <int> p;
    long long ans2=0;
    for (int i=1;i<=t;i++)
    {
        for (int j=0;j<=3;j++)
        {
            cin>>x[j];
        }
        for (int j=1;j<=n;j++)
        {
            now[j]=x[j%4]^b[j];
            //cout<<now[j]<<'\n';
        }
        for (int j=1;j<=m;j++)
        {
            if (ans[q[j]]) ans2^=ans[q[j]]*j;
            for (int k=1;k<=q[j];k++)
            {
                p.push(k);
            }
            int ans1=1,num1=0;
            while(ans1<q[j])
            {
                ans1*=2;
                num1++;
            }
            //cout<<" "<<num1<<'\n';
            for (int j1=1;j1<=num1;j1++)
            {
                int l=p.size();
                for (int k=1;k<=l/2;k++)
                {
                    int p1=p.front();
                    p.pop();
                    int p2=p.front();
                    p.pop();
                    //cout<<p1<<" "<<p2<<endl;
                    //cout<<a[j1][k-1]<<" ";
                    if (a[j1][k-1]=='0')
                    {
                        if (now[p1]>=j1) p.push(p1);
                        else p.push(p2);
                    }
                    else{
                        if (now[p2]>=j1) p.push(p2);
                        else p.push(p1);
                    }
                }


            }
            //cout<<p.front()<<" ";
            ans[q[j]]=p.front();
            //cout<<p.front()*j<<" ";
            ans2=ans2^(p.front()*j);
            p.pop();
        }
        memset(ans,0,sizeof(ans));


    }
    cout<<ans2;
    return 0;

}
